<template>
  <div class="adjust-load-container">
    <h4>负载调节控制</h4>
    <p class="status-info" v-if="isOverloadActive">当前检测到运行过载状态</p>
    <div class="button-group">
      <el-button
        type="primary"
        @click="adjustLoad"
        :loading="isAdjusting"
        size="large"
        class="adjust-button"
      >
        调节负载
      </el-button>
      <p class="instruction">点击按钮可将负载参数重置为正常值</p>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { ElButton, ElMessage } from "element-plus";
import { setAlgorithmParam } from "@/api/util";
import { useUserStore } from "@/store/user";

// 定义组件props
const props = defineProps({
  isOverloadActive: {
    type: Boolean,
    default: false,
  },
});

// 获取用户store实例
const userStore = useUserStore();

// 定义组件事件
const emit = defineEmits(["loadAdjusted"]);

const isAdjusting = ref(false);

// 调节负载函数
const adjustLoad = async () => {
  try {
    isAdjusting.value = true;
    // 从user store获取copyNum
    const copyNum = userStore.copyNum;
    // 调用setAlgorithmParam设置"2_18_0_0"值为0，传入copyNum参数
    await setAlgorithmParam("2_18_0_0", 0, copyNum);

    // 显示成功提示
    ElMessage.success("负载调节成功");

    // 触发负载已调节事件
    emit("loadAdjusted");
  } catch (error) {
    console.error("调节负载失败:", error);
    ElMessage.error("负载调节失败，请稍后重试");
  } finally {
    isAdjusting.value = false;
  }
};
</script>

<style scoped>
.adjust-load-container {
  background: #2c3e50;
  border-radius: 8px;
  padding: 20px;
  color: #ecf0f1;
}

.adjust-load-container h4 {
  color: #3498db;
  margin-top: 0;
  margin-bottom: 16px;
  padding-bottom: 10px;
  border-bottom: 1px solid #34495e;
  font-size: 18px;
  font-weight: 600;
}

.status-info {
  background-color: #e74c3c;
  color: white;
  padding: 12px;
  border-radius: 6px;
  margin-bottom: 20px;
  font-weight: 500;
  display: flex;
  align-items: center;
}

.status-info:before {
  content: "⚠️";
  margin-right: 8px;
}

.button-group {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.adjust-button {
  background: linear-gradient(135deg, #27ae60, #2ecc71);
  border: none;
  padding: 12px 32px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 16px;
}

.adjust-button:hover {
  background: linear-gradient(135deg, #229954, #27ae60);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(46, 204, 113, 0.4);
}

.adjust-button:active {
  transform: translateY(0);
}

.adjust-button.is-loading {
  background: #95a5a6;
}

.instruction {
  color: #95a5a6;
  font-size: 14px;
  text-align: center;
  margin: 0;
}

@media (max-width: 768px) {
  .adjust-load-container {
    padding: 16px;
  }

  .adjust-button {
    padding: 10px 24px;
    font-size: 14px;
    width: 100%;
  }
}
</style>